package com.qmrz.utils.http;


import cn.hutool.cache.CacheUtil;
import cn.hutool.cache.impl.TimedCache;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.qmrz.utils.constants.DbConstant;
import com.qmrz.utils.sign.SignUtil;

public class HttpUtil {

    private static final Log log = LogFactory.get();

    //缓存 token 四个小时过期
    private static  TimedCache<String, String> tokenCaheMap = CacheUtil.newTimedCache(14400000);


    /**
     * 获取token
     * @return
     */
    public static String getToken(){
        String token =  tokenCaheMap.get(DbConstant.appId);
        if (!StrUtil.isEmpty(token)){
            return token;
        }
        //josn字符串加入计算所以json要保证有序
        JSONObject req = new JSONObject(true);
        req.set("appId",DbConstant.appId);
        //随机数 32位
        req.set("salt",IdUtil.simpleUUID());
        //当前毫秒数
       // req.set("timestamp",StrUtil.toString(System.currentTimeMillis()/1000) );
        req.set("timestamp",System.currentTimeMillis()/1000);
       // req.set("timestamp",1651044006);
        //生成最终sign
        String sign = SignUtil.createSign(req.toString(),DbConstant.appSecret);
        req.set("sign",sign);

        log.info("getToken req: {}",req.toString());
        String result = HttpRequest.post(DbConstant.getTokenUrl)
                .body(req.toString())
                .header(Header.CONTENT_TYPE, "application/json")
                .timeout(20000)
                .execute().body();
        log.info("getToken result: {}",result);

        JSONObject jsonObject = JSONUtil.parseObj(result);
        if (jsonObject.getInt("code") == 0) {
            JSONObject resultJsonObject  = JSONUtil.parseObj(jsonObject.get("result"));
            token = resultJsonObject.getStr("token");
            tokenCaheMap.put(DbConstant.appId,token);
        }
        return token;
    }


    /**
     * 举例 ：查询房屋信息
     * @return
     */
    public static void queryHouse(){

        JSONObject req = new JSONObject(true);
        req.set("custname","");
        req.set("pageNum",1);
        req.set("pageSize",1100);

        log.info("queryHouse req: {}",req.toString());
        String result = HttpRequest.post(DbConstant.queryHouseUrl)
                .body(req.toString())
                .header(Header.CONTENT_TYPE, "application/json")
                .header("Authorization", "Bearer "+getToken())
                .timeout(20000)
                .execute().body();
        log.info("queryHouse result: {}",result);

    }

    /**
     * 查询钥匙
     * @return
     */
    public static void queryKey(){
        JSONObject req = new JSONObject(true);
        req.set("custname","");
        req.set("pageNum",2);
        req.set("pageSize",5);
        log.info("queryKey req: {}",req.toString());
        String result = HttpRequest.post(DbConstant.queryKeyUrl)
                .body(req.toString())
                .header(Header.CONTENT_TYPE, "application/json")
                .header("Authorization", "Bearer "+getToken())
                .timeout(20000)
                .execute().body();
        log.info("queryKey result: {}",result);
    }

    /**
     * 发放钥匙
     * @return
     */
    public static void giveKey(){
        JSONObject req = new JSONObject(true);
        req.set("houseid","1b290fd71f1c47c6bc88f176c95b6e96");
        req.set("lockid","AILOCK_03b7d9e364c4");
        req.set("account","15994070110");
        req.set("startdate","2022-10-19 14:39:34");
        req.set("expireddate","2023-10-19 14:39:34");
        log.info("queryKey req: {}",req.toString());
        String result = HttpRequest.post(DbConstant.giveKeyUrl)
                .body(req.toString())
                .header(Header.CONTENT_TYPE, "application/json")
                .header("Authorization", "Bearer "+getToken())
                .timeout(20000)
                .execute().body();
        log.info("queryKey result: {}",result);
    }

    /**
     * 修改钥匙
     * @return
     */
    public static void updateKey(){
        JSONObject req = new JSONObject(true);
        req.set("houseid","1b290fd71f1c47c6bc88f176c95b6e96");
        req.set("lockid","AILOCK_03b7d9e364c4");
        req.set("keyid","0000433665f341dda3ae1156938a855a");
        req.set("startdate","2022-10-19 14:39:34");
        req.set("expireddate","2023-10-19 14:39:34");
        log.info("queryKey req: {}",req.toString());
        String result = HttpRequest.post(DbConstant.updateKey)
                .body(req.toString())
                .header(Header.CONTENT_TYPE, "application/json")
                .header("Authorization", "Bearer "+getToken())
                .timeout(20000)
                .execute().body();
        log.info("queryKey result: {}",result);
    }



}
